import { _decorator, AssetManager, assetManager, Canvas, Component, director, loader, Node, Primitive, SceneAsset, tween, view, screen } from 'cc';
import { GameCtx } from '../logic/GameCtx';
import { UIMgr } from '../../framework/ui/UIMgr';
import ViewDefine from '../ui/ViewDefine';
import AssetMgr from '../../framework/asset/AssetMgr';
import GlobalData from '../GlobalData';
import { ResourceNames } from '../constant/CommonConstant';
import { LogMgr } from '../../framework/log/LogMgr';
import SkillCfgRoot from '../data/static/SkillCfgRoot';
const { ccclass, property } = _decorator;
// import { Observable } from "rxjs";

@ccclass('SceneMain_Launch')
export class SceneMain_Launch extends Component {
    protected onLoad() {
        //加载启动资源
        let startLoadTasks:Promise<AssetManager.Bundle>[] = []
        //加载ui资源
        let uiTask = AssetMgr.Ins.loadBundle(ResourceNames.StartResources)
        startLoadTasks.push(uiTask)
        // let strean$= new Observable((subscribe)=>{
        //     AssetMgr.Ins.loadBundle(ResourceNames.StartResources)
        // })
        // strean$.subscribe({
        //     complete:()=>{
        //         console.log("资源加载完成")
        //     },
        // })
        Promise.all(startLoadTasks).then(async () => {
            //适配屏幕
            this.fitScreen();
            //初始化UI管理器
            let canvas = director.getScene().getChildByName('UICanvas').getComponent(Canvas);
            UIMgr.Ins.init(canvas);
            //显示加载界面
            await UIMgr.Ins.show(ViewDefine.LoadingView)
            //开始加载其他资源
            //加载UI资源
            GlobalData.Ins.loadingViewData.loadingPercent = 1;  
            GlobalData.Ins.loadingViewData.loadingText = "加载UI资源中...";  
            await AssetMgr.Ins.loadBundle(ResourceNames.UIResources)
            GlobalData.Ins.loadingViewData.loadingPercent = 5;  
            //加载配置表
            GlobalData.Ins.loadingViewData.loadingText = "加载配置资源中...";  
            await AssetMgr.Ins.loadConfigBundle()

            GlobalData.Ins.loadingViewData.loadingPercent = 10;  
            //加载预制件
            GlobalData.Ins.loadingViewData.loadingText = "加载预制件资源中...";  
            await AssetMgr.Ins.loadBundle(ResourceNames.PrefabResources)

            GlobalData.Ins.loadingViewData.loadingPercent = 30;  

            this.startGame();
        });
    
    }
    fitScreen() {
        // 获取设计分辨率
        const designSize = view.getDesignResolutionSize();
        const designWidth = designSize.width;
        const designHeight = designSize.height;

        // 获取屏幕分辨率
        const screenSize = view.getVisibleSize();
        const screenWidth = screenSize.width;
        const screenHeight = screenSize.height;

        // 计算宽高比
        const designRatio = designWidth / designHeight;
        const screenRatio = screenWidth / screenHeight;

        // 获取canvas节点
        const canvas = director.getScene().getChildByName('UICanvas');
        if (!canvas) return;

        // 设置适配策略
        if (screenRatio > designRatio) {
            // 宽屏设备，以高度为基准
            canvas.setScale(1, screenHeight / designHeight);
        } else {
            // 窄屏设备，以宽度为基准
            canvas.setScale(screenWidth / designWidth, 1);
        }

        // 重新定位canvas
        canvas.setPosition(screenWidth / 2, screenHeight / 2);
    }
    async startGame() {
        let skillCfg = SkillCfgRoot.Ins.cfgMap
        LogMgr.debug(skillCfg)
        //检查资源更新
        //初始化框架的各个模块，资源管理，UI管理，网络管理，日志管理，声音管理等
        //配置表
        //加载其他资源
        //第三方登录
        //加载主场景
        GlobalData.Ins.loadingViewData.loadingText = "加载主场景中...";  
        director.preloadScene('Scene_Main', (completedCount: number, totalCount: number, item: any) => {
        }, async (error: null | Error, sceneAsset?: SceneAsset) => {
            if (error) {
                GlobalData.Ins.loadingViewData.loadingText = "加载主场景失败！";  
                LogMgr.err('场景 SceneMain 加载失败', error);
                this.scheduleOnce(()=>{
                    //退出游戏
                    director.end()
                },5000)

                return;
            }
            GlobalData.Ins.loadingViewData.loadingPercent = 100;  
            director.loadScene("Scene_Main", async (error: Error, scene: any) => {
                if (error) {
                    console.log('场景 SceneMain 加载失败', error);
                } else {
                    console.log('显示 SceneMain');
                    await UIMgr.Ins.show(ViewDefine.MainView);
                    await UIMgr.Ins.hide(ViewDefine.LoadingView);
                    console.log('显示 SceneMain');
                }
            });
        });
        //初始化游戏逻辑代码
        //加载游戏资源
        //启动逻辑
        //进入游戏代码的入口逻辑
        // GameCtx.instance.gameStart();
    }


    update(deltaTime: number) {

    }
}
